<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous" />
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
    <div class="container-fluid" id="app">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12 text-center" style="background-color: red; color: white" >
                    <h3>蜗牛书店</h3>
                </div>
            </div>

            <div class="row" style="margin-top: 10px">
                <div class="col-md-6">
                    <ol class="breadcrumb">
                        <li v-for="bt in typelist"><a :href="'/book/showBookView/?typeid='+bt.id" v-text="bt.name"></a></li>
                    </ol>
                </div>

                <div class="col-md-3">
                    <form th:action="@{/book/searchView}" method="post" class="form-inline">
                        <div class="form-group">
                            <label for="search" class="sr-only">请输入关键词</label>
                            <input type="text" name="searchName" class="form-control" id="search" placeholder="请输入关键词">
                        </div>
                        <button type="submit" class="btn btn-default">搜索</button>
                    </form>
                </div>

                <div class="col-md-3">
                    <span v-if="account == ''">
                        <a class="btn btn-success" th:href="@{/login.html}">登录</a>
                        <a class="btn btn-primary" th:href="@{/reg.html}">注册</a>
                    </span>

                    <span v-if="account != ''">
                        欢迎您: {{account}}  &nbsp;&nbsp;
                        <a class="btn btn-primary" href="">退出</a>
                    </span>

                    <button type="button" class="btn btn-default btn-sm">个人中心</button>
                    <a   class="btn btn-default btn-sm" th:href="@{/cart.html}">我的购物车</a>
                    <a  class="btn btn-default btn-sm" th:href="@{/order.html}">我的订单</a>
                </div>
            </div>
        </div>

        <div class="container">

          <!--  <div v-for="top in topTenList" style="float: left; border: 1px solid #cccccc">
                <div>
                    <img :src="top.imgsrc" alt="图片" class="img-rounded">
                </div>
                <div>
                    <p>书名 : <span v-text="top.name"></span>  </p> 价格 :<span v-text="top.price"></span></p>
                </div>
            </div>-->
            <div class="row">
                <div class="col-md-12">
                    <p style="background-color: red ; color: white; font-size: 30px">图书销售排行榜</p>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6 col-md-4" v-for="(top,index) in topTenList">
                    <div class="thumbnail">
                        <a :href="'./book/singleBook?bid='+top.id"><img :src="top.imgsrc" alt="..."></a>
                        <div class="caption text-center">
                            <p>书名:<span v-text="top.name"></span></p>
                            <p>价格:<span v-text="top.price"></span></p>
                            <!--<span v-text="index"></span>-->
                            <p>
                                <a class="btn btn-primary" :href="'/book/singleBook?bid='+top.id">详情</a>  &nbsp;&nbsp;&nbsp;&nbsp;
                                <a class="btn btn-primary" href="javascript:void(0)" @click="addToCart(top.id)">加入购物车</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <div class="container">
            ========={{account}}============
        </div>
    </div>

    <script>
        let vueApp = new Vue({
            el:"#app",
            data:{
                typelist:[],
                topTenList:[],
                account:''
            },
            methods:{
                //初始化图书类型的数据
                initBookType(){
                    let _this = this ;
                    let url = "./home/typeList" ;
                    $.get(url,function (data){
                        console.log(data);
                        _this.typelist = data ;
                    },"json")
                },
                initTopTen(){
                    let _this = this ;
                    let url = "./home/topTen" ;
                    $.get(url,function (data){
                        _this.topTenList = data ;
                    },"json");
                } ,
                //获取用户名
                getAccount(){
                    let url = "/user/getAccount" ;
                    let _this = this ;
                    $.get(url,function (data){
                        _this.account = data ;
                    })
                },
                addToCart(id){
                    //alert(id);
                    let url = "/cart/addToCart" ;
                    let _this = this ;
                    $.post(url,{bookId:id},function (data){
                        if (data == 'ok'){
                            alert("添加成功!")
                        }else {
                            alert("添加失败!");
                        }
                    });
                }
            },
            created(){
                this.initBookType();
                this.initTopTen();
                this.getAccount();
            }
        });
    </script>
</body>
</html>